package com.tianhui.consignortwofei.mvp.ui.activity.auth;

import android.content.Intent;
import android.widget.Button;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.fgs.common.CommonFragment;
import com.fgs.common.widget.itemView.InputItemView;
import com.fgs.common.widget.itemView.RegionItemView;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.base.BaseActivity;
import com.tianhui.consignortwofei.mvp.contract.AuthContract;
import com.tianhui.consignortwofei.mvp.model.AuthenticationModel;
import com.tianhui.consignortwofei.mvp.model.enty.ConsignorInfo;
import com.tianhui.consignortwofei.mvp.presenter.AuthPresenter;
import com.tianhui.consignortwofei.other.PictureType;
import com.tianhui.consignortwofei.other.auth.AuthInfoProcess;
import com.tianhui.consignortwofei.other.auth.PictureContext;
import com.tianhui.consignortwofei.other.auth.PictureStrategyFactory;
import com.tianhui.consignortwofei.widget.AuthView;

import butterknife.BindView;

/**
 * @author fgs
 * @date 2021/1/7
 * @description
 **/
public abstract class BaseAuthActivity extends BaseActivity<AuthContract.View, AuthContract.Presenter> implements AuthContract.View {

    @BindView(R.id.activity_all_auth_info_driverContentLinearLayout)
    LinearLayout driverContentLinearLayout;
    @BindView(R.id.layout_consignor_auth_frontAuthView)
    AuthView idCardFrontAuthView;
    @BindView(R.id.layout_consignor_auth_backAuthView)
    AuthView idCardBackAuthView;
    @BindView(R.id.layout_consignor_auth_businessLicenseAuthView)
    AuthView businessLicenseAuthView;

    @BindView(R.id.layout_consignor_auth_legalNameInputItemView)
    InputItemView legalNameInputItemView;
    @BindView(R.id.layout_consignor_auth_legalIdNumberInputItemView)
    InputItemView legalIdNumberInputItemView;

    @BindView(R.id.layout_consignor_auth_legalPhoneInputItemView)
    InputItemView legalPhoneInputItemView;

    @BindView(R.id.layout_consignor_auth_consignorNameInputItemView)
    InputItemView consignorNameInputItemView;
    @BindView(R.id.layout_consignor_auth_merchantNameInputItemView)
    InputItemView merchantNameInputItemView;
    @BindView(R.id.layout_consignor_auth_merchantPhoneInputItemView)
    InputItemView merchantPhoneInputItemView;
    @BindView(R.id.layout_consignor_auth_contractNameInputItemView)
    InputItemView contractNameInputItemView;

    @BindView(R.id.layout_consignor_auth_contractPhoneInputItemView)
    InputItemView contractPhoneInputItemView;
    @BindView(R.id.layout_consignor_auth_chargeNameInputItemView)
    InputItemView chargeNameInputItemView;
    @BindView(R.id.layout_consignor_auth_chargePhoneInputItemView)
    InputItemView chargePhoneInputItemView;
    @BindView(R.id.layout_consignor_auth_passwordInputItemView)
    InputItemView passwordInputItemView;

    @BindView(R.id.layout_consignor_auth_feeInputItemView)
    InputItemView feeInputItemView;
    @BindView(R.id.layout_consignor_auth_addressRegionItemView)
    RegionItemView addressRegionItemView;

    @BindView(R.id.layout_consignor_auth_taxAuthorityInputItemView)
    InputItemView taxAuthorityInputItemView;


    @BindView(R.id.layout_consignor_auth_commitButton)
    Button consignorCommitButton;


    protected ConsignorInfo mConsignorAuthInfo;
    protected AuthenticationModel mModel;


    protected boolean isIdCardFrontSuccess;
    protected boolean isIdCardBackSuccess;
    protected boolean isBusinessLicenseSuccess;

    protected AuthInfoProcess mAuthInfoProcess;

    protected PictureStrategyFactory pictureStrategyFactory;
    protected PictureContext pictureContext;

    @Override
    public int getLayoutId() {
        return R.layout.activity_all_auth_info;
    }

    @Override
    public AuthContract.Presenter createPresenter() {
        return new AuthPresenter(this);
    }

    @Override
    public AuthContract.View createView() {
        return this;
    }

    @Override
    public void init() {
        mConsignorAuthInfo = new ConsignorInfo();

        mModel = new AuthenticationModel();

        mAuthInfoProcess = new AuthInfoProcess();

        pictureStrategyFactory = new PictureStrategyFactory();
    }

    @Override
    public CommonFragment createFragment() {
        return null;
    }


    public void requestDriverInfoCommit(ConsignorInfo info) {
        requestDriverInfoCommit(info, 0);
    }

    public void requestDriverInfoCommit(ConsignorInfo info, int type) {
        getPresenter().requestConsignorInfoCommit(info, type);
    }

    public void commitSuccess(int type) {
        switch (type) {
            case PictureType.TYPE_FRONT:
                isIdCardFrontSuccess = true;
                break;
            case PictureType.TYPE_BACK:
                isIdCardBackSuccess = true;
                break;
            case PictureType.TYPE_BUSINESS_LICENSE:
                isBusinessLicenseSuccess = true;
                break;

        }
    }

    public void commitFailed(int type) {
        switch (type) {

            case PictureType.TYPE_FRONT:
//                isIdCardFrontSuccess = false;
                if (idCardFrontAuthView != null)
                    idCardFrontAuthView.showFailedStatus();
                break;
            case PictureType.TYPE_BACK:
//                isIdCardBackSuccess = false;
                if (idCardBackAuthView != null)
                    idCardBackAuthView.showFailedStatus();
                break;
            case PictureType.TYPE_BUSINESS_LICENSE:
//                isDriverLicenseSuccess = false;
                if (businessLicenseAuthView != null)
                    businessLicenseAuthView.showFailedStatus();
                break;
        }
    }


    protected boolean checkConsignorInfoProgress() {
        if (!isIdCardFrontSuccess) {
            showToast("身份证正面照片尚未上传完成");
            commitFailed(PictureType.TYPE_FRONT);
            return false;
        }
        if (!isIdCardBackSuccess) {
            showToast("身份证反面照片尚未上传完成");
            commitFailed(PictureType.TYPE_BACK);
            return false;
        }
        if (!isBusinessLicenseSuccess) {
            showToast("营业执照照片尚未上传完成");
            commitFailed(PictureType.TYPE_BUSINESS_LICENSE);
            return false;
        }
        return true;
    }


    @Override
    public void showConsignorAuthenticationInfo(ConsignorInfo driverAuthInfo) {

    }

    @Override
    public void showCommitSuccess(int type) {
        commitSuccess(type);
    }

    @Override
    public void showCommitFailed(int type) {
        commitFailed(type);
    }


    @Override
    public void commitSuccess() {
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (pictureContext != null)
            pictureContext.onActivityResult(requestCode, resultCode, data);
    }

}
